/*
 *   Copyright©2023 JoyCastle.
 *   All Rights Reserved.
 *   
 *   FileName:     UIShopPaypalItem.cs
 *   Author:       Administrator
 *   CreateTime:   2024/04/29 15:49:45
 *   UnityVersion: 2020.3.48f1c1
 *   Version:      1.1.9
 *   Description:
 *   
 */

using System.Collections;
using System.Collections.Generic;
using offline2048;
using GTA;
using MotionFramework.Event;
using UnityEngine;
using UnityEngine.UI;

public class UIShopPaypalItem: MonoBehaviour
{
    private Image m_ImgProgress;
    private Text m_TxtDoller;
    private Text m_TxtHaveDoller;
    private Button m_BtnItem;

    private Button m_BtnRestore;
    private GameObject m_BtnNumber;
    //固定文字处理=========================================================
    private Text m_ContentUsd;
    
    private int m_Index;
    public void InitData(int index)
    {
        InitView();
        InitText();
        m_Index = index;
        SetViewData();
    }

    private void InitView()
    {
        m_ImgProgress = this.transform.Find("DownLoadingBg/Loading").GetComponent<Image>();
        m_TxtDoller = this.transform.Find("ContentNumber").GetComponent<Text>();
        m_TxtHaveDoller = this.transform.Find("NumberBtn/QuickTrack").GetComponent<Text>();
        m_BtnItem = this.transform.Find("ItemFrame").GetComponent<Button>();

        m_BtnRestore = this.transform.Find("RestoreBtn").GetComponent<Button>();
        m_BtnNumber = this.transform.Find("NumberBtn").gameObject;
        
        //固定文字处理=========================================================
        m_ContentUsd = this.transform.Find("ContentUsd").GetComponent<Text>();
        
        m_BtnItem.onClick.AddListener(OnClickItemBtn);
        m_BtnRestore.onClick.AddListener(OnClickRestoreBtn);
    }

    private void InitText()
    {
        m_ContentUsd.text = Utils.GetTextByID("900045");
    }
    
    public void ResetData()
    {
        SetViewData();
    }
    
    private void SetViewData()
    {
        List<ShopListItemData> _shopListItemDatas = UIShopManager.Instance.GetShopListItemData();
        if (_shopListItemDatas[m_Index - 1].m_IsRestore == 0)
        {
            m_BtnItem.enabled = true;
            m_BtnNumber.SetActive(true);
            m_BtnRestore.gameObject.SetActive(false);
        }
        else
        {
            m_BtnItem.enabled = false;
            m_BtnNumber.SetActive(false);
            m_BtnRestore.gameObject.SetActive(true);
        }
        
        if (PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT) > 1000)
        {
            m_TxtHaveDoller.text = (PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT) / 1000) + "k";   
        }
        else
        {
            m_TxtHaveDoller.text = (PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT)).ToString();
        }
        m_TxtDoller.text = $"{Utils.GetTextByID("900044")} {m_Index * 1000}";
        m_ImgProgress.fillAmount = (PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT)) / (m_Index * 1000);
    }
    
    private void OnClickItemBtn()
    {
        //EventManager.Instance.SendMessage(new NewGuideEvent.HideGuideEvent());
        ShopEvent.OnTouchItem _event = new ShopEvent.OnTouchItem();
        _event.m_Index = m_Index;
        EventManager.Instance.SendMessage(_event);
    }

    private void OnClickRestoreBtn()
    {
        ShopEvent.OnTouchReStore _event = new ShopEvent.OnTouchReStore();
        _event.m_Index = m_Index;
        EventManager.Instance.SendMessage(_event);
    }
}

